package logs

import (
	"os"
	"strings"
)

// 全局logger
var (
	Out ILog = NewWriterLog(os.Stdout, DebugLog).CallerSkip(01)
)

func SetLogger(log ILog) {
	Out = log
}

type LogLevel int

const (
	DebugLog LogLevel = iota
	TraceLog
	InfoLog
	WarnLog
	ErrorLog
	PanicLog
	FatalLog
)

const (
	DebugLevel LogLevel = DebugLog
	TraceLevel          = TraceLog
	InfoLevel           = InfoLog
	WarnLevel           = WarnLog
	ErrorLevel          = ErrorLog
	PanicLevel          = PanicLog
	FatalLevel          = FatalLog
)

func (l LogLevel) String() string {
	switch l {
	case TraceLog:
		return "Trace"
	case DebugLog:
		return "Debug"
	case InfoLog:
		return "Info"
	case WarnLog:
		return "Warn"
	case ErrorLog:
		return "Error"
	case FatalLog:
		return "Fatal"
	case PanicLog:
		return "Panic"
	}
	return "Debug"
}

var LevelMap = map[LogLevel]string{
	TraceLog: "Trace",
	DebugLog: "Debug",
	InfoLog:  "Info",
	WarnLog:  "Warn",
	ErrorLog: "Error",
	FatalLog: "Fatal",
	PanicLog: "Panic",
}
var LevelMapReverse = map[string]LogLevel{
	"Trace": TraceLog,
	"Debug": DebugLog,
	"Info":  InfoLog,
	"Warn":  WarnLog,
	"Error": ErrorLog,
	"Fatal": FatalLog,
	"Panic": PanicLog,
}

// LogLevelMap 兼容旧配置
var LogLevelMap = LevelMap

func NameToLogLevel(s string) LogLevel {
	switch strings.ToLower(s) {
	case "trace":
		return TraceLog
	case "debug":
		return DebugLog
	case "info":
		return InfoLog
	case "warn":
		return WarnLog
	case "error":
		return ErrorLog
	case "fatal":
		return FatalLog
	case "panic":
		return PanicLog
	}
	return DebugLog
}

const DefaultLogPath = "./runtime/logs/app.log"
